package com.chen.mkarm

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.chen.mkarm.ui.theme.MkarmTheme

/////////////////////////////////////////////////////////////////////////////////
//----------------------------串口页组件--------------------------------------------
@Composable
fun SerialPortScreen(logs: List<String>, senddata: (String) -> Unit, modifier: Modifier = Modifier) {
    Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
        Text(text = "串口数据接收")
        Log.i("ms.chen", "logs:$logs")
        SerialDataScrollableList(logs)
        ShadowedBoxSendSerialData(senddata)
    }
}

@Composable
fun ShadowedBoxSendSerialData(senddata: (String) -> Unit) {
    Card(modifier = Modifier
        .height(150.dp)
        .fillMaxSize()
        .padding(4.dp)) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.padding(16.dp)
        ) {
            Column {
                // 用于存储新输入项的状态变量
                var newItem by remember { mutableStateOf("") }

                // 提供一个文本输入框让用户输入新项
                TextField(value = newItem, onValueChange = { newItem = it }, label = { Text("Enter new item") })

                // 提供一个按钮来添加新项到列表中
                Button(onClick = {
                    if (newItem.isNotBlank()) {
                        //发送串口数据到机器人
                        println("发送串口数据到机器人：$newItem")
                        senddata(newItem)
                        newItem = "" // 清空输入框
                    }
                }) {
                    Text("发送")
                }
            }
        }
    }
}

@Composable
fun SerialDataScrollableList(items: List<String>) {
    LazyColumn(modifier = Modifier
        .height(400.dp)
        .fillMaxWidth()
        .background(color = Color.Green)) {
        // 使用items扩展函数来遍历和渲染列表中的每一项
        items(items) { item ->
            Text(
                text = item,
                style = androidx.compose.ui.text.TextStyle(textAlign = TextAlign.Start)
            )
        }
    }
}


@Preview(showBackground = true, name="view1", widthDp = 320)
@Composable
fun SerialPreview() {
    MkarmTheme {
        //SerialScreen()
    }
}